
package com.dudsbury.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JTable;

import org.joda.time.DateTime;

import com.dudsbury.database.Booking;
import com.dudsbury.database.MyDateTime;
import com.dudsbury.database.MyRoom;
import com.dudsbury.jtablestuff.CustomTable;
import com.dudsbury.jtablestuff.JScrollPaneAdjuster;
import com.dudsbury.jtablestuff.JTableRowHeaderResizer;
import com.dudsbury.jtablestuff.RowHeaders;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.io.IOException;

import javax.swing.SwingConstants;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;

public class MainFrame extends JFrame {

	private JPanel contentPane,panel_1;
	public static CustomTable table;
	private JScrollPane pane;
	

	public MainFrame(ArrayList<Booking> bookingList,MyRoom[] roomList) {
		setTitle("Dudsbury Hotel Event Booking System");
		setJMenuBar(new CustomFileMenu());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(1080,640);
		setExtendedState(this.getExtendedState()|JFrame.MAXIMIZED_BOTH);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null,
				null));
		contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JButton btnViewByWeek = new JButton("View By Week");
		btnViewByWeek.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				table.setWeeklyView();
			}
		});
		panel.add(btnViewByWeek);

		JButton btnViewByMonth = new JButton("View By Month");
		btnViewByMonth.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				table.setMonthlyView();
			}
		});
		panel.add(btnViewByMonth);

		JButton btnSelectDate = new JButton("Select Date");
		btnSelectDate.setIcon(new ImageIcon("trunk/Dudsbury/src/res/calendericon3.png"));
		btnSelectDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new CalendarFrame(table);
			}
		});
		panel.add(btnSelectDate);

		panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		
		
		table = new CustomTable(bookingList,roomList);
		RowHeaders rowHeader = new RowHeaders(roomList,table);
		
		 pane = new JScrollPane(table);
		 //pane.setViewportView(table);
		 pane.setRowHeaderView(rowHeader);
		 JTableHeader corner = rowHeader.getTableHeader();
	        corner.setReorderingAllowed(false);
	        corner.setResizingAllowed(false);
	        new JScrollPaneAdjuster(pane);

	        new JTableRowHeaderResizer(pane).setEnabled(true); 
	        
		panel_1.add(pane);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		contentPane.add(panel_2, BorderLayout.WEST);

		JButton button = new JButton("");
		button.setIcon(new ImageIcon("trunk/Dudsbury/src/res/leftarrowicon3.png"));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				if (table.getCurrentView()==table.WEEKLY_VIEW) {
					DateTime datehold = table.getCurrentDate().getDateTime().minusDays(7);
					table.setCurrentDate(new MyDateTime(datehold));
					table.setWeeklyView();
					
				}
				else {
					DateTime datehold = table.getCurrentDate().getDateTime().minusDays(
							table.getCurrentDate().getDateTime().dayOfMonth().getMaximumValue());
					table.setCurrentDate(new MyDateTime(datehold));
					table.setMonthlyView();
				}
				
			}
		});
		panel_2.add(button);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		contentPane.add(panel_3, BorderLayout.EAST);

		JButton button_1 = new JButton("");
		button_1.setIcon(new ImageIcon("trunk/Dudsbury/src/res/rightarrowicon.png"));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (table.getCurrentView()==table.WEEKLY_VIEW) {
					DateTime datehold = table.getCurrentDate().getDateTime().plusDays(7);
					table.setCurrentDate(new MyDateTime(datehold));
					table.setWeeklyView();
					
				}
				else {
					DateTime datehold = table.getCurrentDate().getDateTime().plusDays(
							table.getCurrentDate().getDateTime().dayOfMonth().getMaximumValue());
					table.setCurrentDate(new MyDateTime(datehold));
					table.setMonthlyView();
				}
				
				
				
			}
		});
		panel_3.add(button_1);
		
		JPanel panel_5 = new JPanel();
		contentPane.add(panel_5, BorderLayout.NORTH);
		
		JLabel lblDudsburyHotelEvent = new JLabel("");
		lblDudsburyHotelEvent.setIcon(new ImageIcon("trunk/Dudsbury/src/res/cropped-banner1.png"));
		lblDudsburyHotelEvent.setHorizontalAlignment(SwingConstants.CENTER);
		lblDudsburyHotelEvent.setFont(new Font("Arial", Font.BOLD, 40));
		
		JPanel panel_4 = new JPanel();
		
		JButton btnEditCustomer = new JButton("New Customer");
		btnEditCustomer.setIcon(new ImageIcon("trunk/Dudsbury/src/res/manicon.png"));
		panel_4.add(btnEditCustomer);
		btnEditCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new CustomerScreen().setVisible(true);
			}
		});
		
		JButton btnCreateMeal = new JButton("New Item");
		btnCreateMeal.setIcon(new ImageIcon("trunk/Dudsbury/src/res/itemicon.png"));
		panel_4.add(btnCreateMeal);
		btnCreateMeal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new CreateItemScreen();
			}
		});
		
		JButton btnNewBooking = new JButton("Search");
		btnNewBooking.setIcon(new ImageIcon("trunk/Dudsbury/src/res/searchicon2.png"));
		panel_4.add(btnNewBooking);
		btnNewBooking.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new SearchScreen();
			}
		});
		
		JPanel panel_6 = new JPanel();
		GroupLayout gl_panel_5 = new GroupLayout(panel_5);
		gl_panel_5.setHorizontalGroup(
			gl_panel_5.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_5.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_5.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_6, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 1034, Short.MAX_VALUE)
						.addComponent(lblDudsburyHotelEvent, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 1034, Short.MAX_VALUE)
						.addComponent(panel_4, GroupLayout.DEFAULT_SIZE, 1034, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panel_5.setVerticalGroup(
			gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_5.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblDudsburyHotelEvent)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		);
		
		JButton btnViewTutorial = new JButton("View Tutorial");
		btnViewTutorial.setIcon(new ImageIcon("trunk/Dudsbury/src/res/help.png"));
		btnViewTutorial.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					new TutorialFrame();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		panel_4.add(btnViewTutorial);
		
		JLabel lblBookingDiary = new JLabel("Booking Diary");
		lblBookingDiary.setFont(new Font("Arial", Font.BOLD, 23));
		lblBookingDiary.setHorizontalAlignment(SwingConstants.CENTER);
		GroupLayout gl_panel_6 = new GroupLayout(panel_6);
		gl_panel_6.setHorizontalGroup(
			gl_panel_6.createParallelGroup(Alignment.LEADING)
				.addComponent(lblBookingDiary, GroupLayout.DEFAULT_SIZE, 1034, Short.MAX_VALUE)
		);
		gl_panel_6.setVerticalGroup(
			gl_panel_6.createParallelGroup(Alignment.LEADING)
				.addComponent(lblBookingDiary, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
		);
		panel_6.setLayout(gl_panel_6);
		gl_panel_5.setAutoCreateContainerGaps(true);
		gl_panel_5.setAutoCreateGaps(true);
		panel_5.setLayout(gl_panel_5);
		
		setVisible(true);
	}
}
